<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { Toastloading } from '@/utils/public';
  import { getDateArray, getPlayEndTime, getSummaries } from '@/utils'
  import xmDatePicker from '@/components/xmDatePicker/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import serachUserId from '@/components/serachUserid/index.vue'
  import table2excel from 'js-table2excel';
  const action = reactive({
    total:0,
    list:[] as any[],
    statistics:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis'))
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    user_id:'',
  })
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await gamesApi.wishLuckUserStatistic({
      date_st,
      date_ed,
      ...listQuery
    })
    loading.close()
    const { list=[], total=0, all={} } = res.data
    action.statistics = [all]
    action.total = total
    action.list = list.map((res:any,index:number)=>{
      const { output_ratio=0, draw_num=0, prop_diamond_num=0, reward_diamond_num=0 } = res
      return {
        ...res,
        index,
        hasChildren:true,
        output_ratio:Number(output_ratio),
        draw_num:Number(draw_num),
        prop_diamond_num:Number(prop_diamond_num),
        reward_diamond_num:Number(reward_diamond_num)
      }
    })
  }
  const tableLoad = (tree: { user_id: any; }, treeNode: any, resolve: (arg0: any) => void)=>{
    const { user_id } = tree
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    gamesApi.childWishLuckUserStatistic({
      user_id,
      date_st,
      date_ed
    }).then(res=>{
      const { data=[] } = res
      resolve(data)
    }).catch(()=>{
      resolve([])
    })
  }
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中')
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await gamesApi.wishLuckUserStatistic({
      page:1,
      date_st,
      date_ed,
      user_id:listQuery.user_id,
      limit:action.total
    })
    loading.close()
    const { list=[] } = res.data
    const data = list.map((res:any)=>{
      res['output_ratio'] = `${res.output_ratio}%`
      return res
    })
    const tHeader = [
      { title: '用户UUID', key: "uuid"},
      { title: '昵称', key: "nickname"},
      { title: '参与次数', key: "draw_num"},
      { title: '总投入（钻石）', key: "prop_diamond_num"},
      { title: '总产出（钻石）', key: "reward_diamond_num"},
      { title: '投入产出比', key: "output_ratio"},
      { title: '损益', key: "profit_loss"},
    ]
    table2excel(
      tHeader,
      data,
      `许愿转盘报表`)
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <serachUserId v-model:user_id="listQuery.user_id" />
        </div>
        <xmDatePicker v-model:timeval="action.timeObj" />
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="warning" @click="handleDownload">导出列表</el-button>
        </div>
      </div>
    </div>
    <el-table
      style="width: max-content;; min-height: 80px"
      class="xm-TableCenter"
      :data="action.statistics"
      border
    >
      <el-table-column width="180" prop="user_num" label="参与人数" />
      <el-table-column width="180" prop="draw_num" label="参与次数" />
      <el-table-column width="180" prop="prop_diamond_num" label="总投入（钻石）" />
      <el-table-column width="180" prop="reward_diamond_num" label="总产出（钻石）" />
      <el-table-column width="180" prop="output_ratio" label="产出比例">
        <template #default="scoped">
          {{ scoped.row.output_ratio }}%
        </template>
      </el-table-column>
    </el-table>
    <el-table
      style="margin-top:40px;"
      class="xm-main xm-TableCenter"
      row-key="index"
      :load="tableLoad"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      lazy
      :data="action.list"
      border
      show-summary
      :summary-method="((param:any)=>getSummaries(param,[1,2],[{key:6,unit:'%'}]))"
    >
      <el-table-column width="120" prop="user_id" label="用户ID" />
      <el-table-column width="120" prop="uuid" label="用户UUID" />
      <el-table-column min-width="150" show-overflow-tooltip prop="nickname" label="昵称" />
      <el-table-column width="180" sortable prop="draw_num" label="参与次数" />
      <el-table-column min-width="120" sortable prop="prop_diamond_num" label="投入（钻石）" />
      <el-table-column min-width="120" sortable prop="reward_diamond_num" label="产出（钻石）" />
      <el-table-column min-width="100" sortable prop="output_ratio" label="投入产出比">
        <template #default="scoped">
          {{ scoped.row.output_ratio }}%
        </template>
      </el-table-column>
      <el-table-column min-width="120" sortable prop="profit_loss" label="损益" />
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
  </div>
</template>
